package org.zhang.controller;

import cn.hutool.core.util.StrUtil;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zhang.common.Result;
import org.zhang.entity.User;
import org.zhang.service.UserService;

import java.util.List;
import java.util.Map;

/**
 * 功能:
 * 作者:小张
 * 日期:2024/5/21 10:55
 */


@CrossOrigin
@RestController
//@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;


    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        return userService.login(user);

    }

    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        return userService.register(user);

    }

//    添加
    @PostMapping("/add")
    public Result add(@RequestBody User user){
        userService.insertUser(user);
        return Result.success(user);

    }

//    修改
    @PutMapping("/updata")
    public Result updata(@RequestBody User user){
        userService.updataUser(user);
        return Result.success(user);

    }

//    根据id删除
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        userService.deleteUser(id);
        return Result.success(id);

    }


//    查询所有
    @GetMapping("/selectAll")
    public Result selectAll(){
        List<User> users = userService.selectAll();
        return Result.success(users);

    }

//    根据id查询
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        User user = userService.selectById(id);
        return Result.success(user);

    }


//    多条件查询
    @GetMapping("/selectMores")
    public Result selectMores(@RequestParam String name,@RequestParam String gender){
        List<User> users = userService.selectMores(name, gender);
        return Result.success(users);

    }

//    模糊查询
    @GetMapping("/selectByLike")
    public Result selectByLike(@RequestParam String name,@RequestParam String gender){
        List<User> byLike = userService.selectByLike(name,gender);
        return Result.success(byLike);

    }
//
//    @GetMapping("/selectByPage")
//    public Result selectByPage(
//            @RequestParam Integer pageNum,
//            @RequestParam Integer pageSize,
//            @RequestParam String name,
//            @RequestParam String gender){
//        Map<String, Object> byPage = userService.selectByPage(pageNum, pageSize, name, gender);
//        return Result.success(byPage);
//
//    }

}
